<template>
  <div>
    <!-- 卡片 -->
    <el-card class="box-card">
      <!-- 搜索框 与 新增框 表单-->
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item label="审批人">
          <el-input
            placeholder="查询订单号"
            style="width: 300px"
            v-model="msg"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            style="margin-left: 20px"
            @click="handlesearch"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 订单信息列表 -->
    <el-table :data="tableData" style="width: 100%" border>
      <el-table-column label="订单号" width="180">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <!-- 订单价格得通过商品表和规格表进行关联 -->
      <el-table-column label="订单价格" width="180">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.price }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否付款" width="180">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.IsPay }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否发货" width="150">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.isSend }}</span>
        </template>
      </el-table-column>

      <el-table-column label="下单时间" width="200">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.createdTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收货地址" width="200">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.address }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleSend(scope.$index, scope.row)"
            >发货</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- Dialog对话框 -->
    <el-dialog title="用户信息" :visible.sync="dialogFormVisible">
      <el-form :model="formData">
        <el-form-item label="发货地址" label-width="100px">
          <el-input
            v-model="formData.address"
            style="width: 400px"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancel">取 消</el-button>
        <el-button type="primary" @click="handleSave">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 分页 -->
    <div class="block">
      <el-pagination
        :current-page="pager.currentPage"
        :page-size="pager.pageSize"
        layout="prev, pager, next, jumper,sizes, total"
        :total="pager.totalSize"
        :page-sizes="[5, 10, 15, 20]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </div>
  </div>
</template>


<script>
import { getlist, getByOrderNum, modUser } from "../api/order";

export default {
  props: {
    isSend: {
      type: Boolean,
    },
  },
  data() {
    return {
      //   isSend:false,
      //初始化一些数据

      currentIndex: 0,
      dialogFormVisible: false,
      pager: {
        //每页显示条目个数
        pageSize: 5,
        currentPage: 1,
        totalSize: 0,
      },
      formData: {
        id: 0,
        username: "",
        password: "",
        remarks: "",
        IsSend: this.isSend,
      },
      tableData: [],
      msg: "",
      curIndex: 0,
    };
  },
  methods: {
    //拉取数据
    fetchData() {
      getlist(this.pager)
        .then((res) => {
          console.log(res);
          this.tableData = res.data.data;
          this.pager = res.data.pager;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    //查询订单
    handlesearch() {
      if (this.msg && this.msg.length > 0) {
        console.log(this.msg);
        getByOrderNum({ data: this.msg })
          .then((data) => {
            console.log(data);
            if (data.code === 1000) {
              //往空数组里放对象的属性值
              this.tableData = data.data;
              this.pager.totalSize = this.tableData.length;
              // this.tableData =[];
              // this.tableData.push(data.data.data);
            } else {
              this.tableData = data.data.data;
            }
          })
          .catch((err) => {
            console.log(err);
          });
      } else {
        this.fetchData();
      }
    },
    // 编辑
    handleEdit(index, row) {
      this.dialogFormVisible = true;
      this.formData.address = row.address;
      this.curIndex = index;
      this.formData.id = row.id;
      this.formData.IsSend = row.isSend;
    },
    // 发货
    handleSend(index, row) {
      console.log(index, row);
      console.log(row.id);
      //通过row.id找到当前的id
      this.$confirm("你确定用户已付款?进行发货吗?, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          console.log("发货成功");
          this.tableData[index].isSend = !this.tableData[index].isSend;
          this.formData.IsSend = !this.formData.IsSend;
          console.log(this.formData);
          this.$message({
            type: "success",
            message: "发货成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消发货",
          });
        });
    },
    // 取消按钮
    handleCancel() {
      this.dialogFormVisible = false;
    },
    // 保存按钮
    handleSave() {
      this.dialogFormVisible = false;
      console.log(this.formData);
      modUser(this.formData.id, this.formData).then((res) => {
        // console.log(res);
        this.tableData.splice(this.curIndex, 1, res.data);
        console.log(this.formData);
        this.fetchData();
      });
    },
    // 通过页码来拉取数据
    handleCurrentChange(val) {
      this.pager.currentPage = val;
      this.fetchData();
    },
    // 改变页大小来拉取数据
    handleSizeChange(val) {
      this.pager.pageSize = val;
      this.fetchData();
    },
  },

  created() {
    this.fetchData();
  },
};
</script>